<template>
  <el-pagination
    :current-page="currentPage"
    :page-sizes="pageSizesWithCurrent"
    :page-size="perPage"
    :total="total"
    background
    layout="total, sizes, prev, pager, next, jumper"
    @size-change="onChangePage('_per_page', ...arguments)"
    @current-change="onChangePage('_page', ...arguments)"
  />
</template>

<script>
export default {
  name: 'AdminPagination',
  props: {
    currentPage: {
      type: Number,
      default: 1,
    },
    pageSizes: {
      type: Array,
      default: () => [15, 20, 30, 50, 100],
    },
    perPage: {
      type: Number,
      default: 15,
    },
    total: {
      type: Number,
    },
  },
  computed: {
    pageSizesWithCurrent() {
      const current = Number(this.$route.query._per_page)

      if (
        !isNaN(current) &&
        current > 0 &&
        this.pageSizes.indexOf(current) === -1
      ) {
        return [current, ...this.pageSizes]
      }

      return this.pageSizes
    },
  },
  methods: {
    onChangePage(type, value) {
      const query = {}
      query[type] = value

      this.$router.push({
        path: this.$route.path,
        query: Object.assign({}, this.$route.query, query),
      })
    },
  },
}
</script>

<style scoped>

</style>
